#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <curses.h>
#include <sys/time.h>
#include <string.h>
#include "caml.h"
#include "structures.h"
#include "display.h"
#include "keyboard.h"
#include "debug.h"
#include "constantes.h"
#include "tools.h"
#include "libvid.h"


value			draw_scene(value			ba,
				   value			tab,
				   value			tab_color,
				   value			color,
				   video			*vid)
{
  suseconds_t		old_usec = 0;
  p_context		c;
  int			i;
  int			*p;
  char			*m;
  int			*getcol;
  int			couleur;
  int			nb_chars;


  p = Data_bigarray_val(ba);
  c = p[IBA_CONTEXT];
  m = Data_bigarray_val(tab);

  caml_release_runtime_system();

  nb_chars = p[IBA_VID_NBCHAR_WIDTH] * p[IBA_VID_NBCHAR_HEIGHT];

  getcol = Data_bigarray_val(tab_color);
  couleur = color;

  wmove(WVIDEO, 0, 0);

  if (!couleur)
    for (i = 1; i < nb_chars; i++)
      waddch(WVIDEO, m[i]);

  else
  {
    for (i = 0; i < nb_chars; i++)
    {
      wattron(WVIDEO, COLOR_PAIR(getcol[i] - 29));
      waddch(WVIDEO, m[i]);
    }
    wattroff(WVIDEO, COLOR_PAIR(getcol[i] - 29)); /* disable color */
  }

  if (IS_FRAME)
    display_frame(WVIDEO);

  touchwin(WVIDEO);
  wrefresh(WVIDEO);

  /* checking ellapsed time */
  old_usec = check_keyboard_state(c, ZONE_MAIN, old_usec, ba, vid);

  caml_acquire_runtime_system();

  return (Val_int(0));
}
